package com.cy.ppj.service.message.impl;

import com.cy.ppj.comm.annotation.Message;
import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.comm.utils.SpringELUtils;
import com.cy.ppj.dao.mapper.message.MessageTemplateMapper;
import com.cy.ppj.model.domian.message.MessageRecord;
import com.cy.ppj.model.domian.message.MessageTemplate;
import com.cy.ppj.model.dto.message.UserRegisterMessageDTO;
import com.cy.ppj.model.dto.wechat.WecahtTemplateDataDTO;
import com.cy.ppj.model.dto.wechat.WechatTemplateDTO;
import com.cy.ppj.model.enums.MessageType;
import com.cy.ppj.model.enums.wechat.WechatTemplate;
import com.cy.ppj.service.wechat.WechatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @description: 用户注册
 * @author: Ocean
 * @create: 2018-08-09 17:26
 **/
@Slf4j
@Service
@Message(type = MessageType.USER_REGISTER)
public class UserRegisterMessageImpl extends MessageServiceImpl {

    @Autowired
    private WechatService wechatService;
    @Autowired
    private MessageTemplateMapper messageTemplateMapper;

    @Override
    public void execute(String message) {

        String mobile = message;

        UserRegisterMessageDTO template = messageTemplateMapper.selectUserRegisterMessageTemplate(mobile);

        //发送站内信
        sendMessage(template);
        //发送微信模版
        sendWechatTemplate(template);

    }

    /**
     * 发送站内信
     *
     * @param template
     */
    private void sendMessage(UserRegisterMessageDTO template) {

        MessageTemplate messageTemplate = messageTemplateService.queryMessageTemplateByTypeInCache(MessageType.USER_REGISTER);
        if (messageTemplate == null) {
            log.info("新成员加入团队[{}],没有消息模版。", template.getMobile());
            return;
        }
        if (!messageTemplate.getOpen()) {
            log.info("新成员加入团队[{}],消息模版未开启。", template.getMobile());
            return;
        }
        if (template.getRecommendUserId() == null) {
            log.info("新成员加入团队[{}],没有推荐人。", template.getMobile());
            return;
        }

        List<MessageRecord> list = new ArrayList<>();

        Map<String, Object> map = new HashMap<>();
        map.put("level", "一级成员");
        map.put("mobile", template.getMobile());
        map.put("companyName", template.getCompanyName());
        map.put("regTime", CommonUtils.dateFormat(template.getCreateTime()));

        String content = SpringELUtils.getValue(messageTemplate.getContent(), map);
        Date time = new Date();

        MessageRecord messageRecord = new MessageRecord();
        messageRecord.setType(MessageType.USER_REGISTER);
        messageRecord.setContent(content);
        messageRecord.setUserId(template.getRecommendUserId());
        messageRecord.setCreateTime(time);
        messageRecord.setUrl("");
        list.add(messageRecord);

        if (template.getSecondRecommendUserId() != null) {

            Map<String, Object> secondMap = new HashMap<>();
            secondMap.put("level", "二级成员");
            secondMap.put("mobile", template.getMobile());
            secondMap.put("companyName", template.getCompanyName());
            secondMap.put("regTime", CommonUtils.dateFormat(template.getCreateTime()));

            content = SpringELUtils.getValue(messageTemplate.getContent(), secondMap);

            MessageRecord secondMessageRecord = new MessageRecord();
            secondMessageRecord.setType(MessageType.USER_REGISTER);
            secondMessageRecord.setContent(content);
            secondMessageRecord.setUserId(template.getSecondRecommendUserId());
            secondMessageRecord.setCreateTime(time);
            secondMessageRecord.setUrl("");

            list.add(secondMessageRecord);
        }

        messageRecordService.addMessageRecords(list);
    }

    /**
     * 发送微信模版
     *
     * @param template
     */
    private void sendWechatTemplate(UserRegisterMessageDTO template) {

        if (template.getOpenId() == null) {
            log.info("业务员[{}]注册微信通知,没有绑定微信,无法发送微信消息。", template.getMobile());
            return;
        }

        String mobile = template.getMobile();

        if (StringUtils.hasLength(template.getCompanyName())) {
            mobile = mobile.concat("-").concat(template.getCompanyName());
        }

        WechatTemplateDTO send = new WechatTemplateDTO();
        send.setOpneId(template.getOpenId());
        send.setAccessToken(wechatService.getAccessToken());
        send.setTemplateId(WechatTemplate.USER_REGISTER.getTemplateId());

        WecahtTemplateDataDTO first = new WecahtTemplateDataDTO();
        first.setValue("您有新的渠道专员注册");

        WecahtTemplateDataDTO keyword1 = new WecahtTemplateDataDTO();
        keyword1.setValue(mobile);

        WecahtTemplateDataDTO keyword2 = new WecahtTemplateDataDTO();
        keyword2.setValue(CommonUtils.dateFormat(new Date()));

        WecahtTemplateDataDTO remark = new WecahtTemplateDataDTO();
        remark.setValue("如有疑问，请咨询客服");

        send.getData().put("first", first);
        send.getData().put("keyword1", keyword1);
        send.getData().put("keyword2", keyword2);

        wechatService.sendTemplateMessage(send);
    }
}
